<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Category;
use Illuminate\Support\Str;

class CategoryController extends Controller
{
    public function index()
    {
        $categories = Category::with('children')->whereNull('parent_id')->get();
        return view('categories.index', compact('categories'));
    }

    public function store(Request $request)
    {

        $request->validate([
            'name' => 'required|unique:categories|max:255',
            'slug' => 'required|unique:categories|max:255',
        ]);

        Category::create([
            'name' => $request->name,
            'description' => $request->description,
            'slug' => $request->slug,
            'parent_id' => $request->parent_id
        ]);

        return redirect()->route('categories.index')->with('success', 'Category created successfully.');
    }

    public function create()
    {
        $categories = Category::with('children')->whereNull('parent_id')->get();
        return view('categories.create', compact('categories'));
    }
}
